package vip.xiaonuo.biz.modular.column.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.biz.modular.column.entity.BizColumn;
import vip.xiaonuo.biz.modular.column.mapper.BizColumnMapper;
import vip.xiaonuo.biz.modular.column.param.BizColumnAddParam;
import vip.xiaonuo.biz.modular.column.param.BizColumnEditParam;
import vip.xiaonuo.biz.modular.column.param.BizColumnIdParam;
import vip.xiaonuo.biz.modular.column.param.BizColumnPageParam;
import vip.xiaonuo.biz.modular.column.service.BizColumnService;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BizColumnServiceImpl extends ServiceImpl<BizColumnMapper, BizColumn> implements BizColumnService {

    @Override
    public void add(BizColumnAddParam addParam) {
        BizColumn column = BeanUtil.copyProperties(addParam, BizColumn.class);
        this.save(column);
    }

    @Override
    public void edit(BizColumnEditParam editParam) {
        BizColumn column = BeanUtil.copyProperties(editParam, BizColumn.class);
        this.updateById(column);
    }

    @Override
    public void delete(List<BizColumnIdParam> idListParam) {
        this.removeBatchByIds(idListParam.stream().map(BizColumnIdParam::getId).collect(Collectors.toList()));
    }

    @Override
    public Page<BizColumn> page(BizColumnPageParam pageParam) {
        LambdaQueryWrapper<BizColumn> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(ObjectUtil.isNotNull(pageParam.getName()),BizColumn::getName,pageParam.getName());
        wrapper.orderByAsc(BizColumn::getSortCode);
        return this.page(CommonPageRequest.defaultPage(), wrapper);
    }

    @Override
    public BizColumn detail(BizColumnIdParam idParam) {
        return this.queryEntity(idParam.getId());
    }

    @Override
    public BizColumn queryEntity(String id) {
        BizColumn column = this.getById(id);
        if (ObjectUtil.isNull(column)){
            throw new CommonException("文章不存在，id值为{}",id);
        }
        return column;
    }

    @Override
    public List<BizColumn> list() {
        LambdaQueryWrapper<BizColumn> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(BizColumn::getSortCode);
        return this.list(wrapper);
    }

    @Override
    public void addViewCount(BizColumnIdParam idParam) {
        BizColumn column = this.queryEntity(idParam.getId());
        if (ObjectUtil.isNull(column.getViewCount())){
            column.setViewCount(0);
        }
        column.setViewCount(column.getViewCount() + 1);
        this.updateById(column);
    }
}
